package com.tengju.payment.domain.shard;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.NonNull;

import java.math.RoundingMode;
import java.util.Comparator;
import java.util.Objects;

public class Amount implements ValueObject<Amount>, Comparable<Amount> {

    private static final long serialVersionUID = -5843905896295865999L;

    @JsonValue
    private final long value;

    /**
     * Instantiates a new Id object.
     *
     * @param value the value
     */

    @JsonCreator
    public Amount(long value) {
        this.value = value;
    }

    public long getValue() {
        return value;
    }


    @Override
    public boolean sameValueAs(Amount other) {
        return other != null && value == other.value;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Amount price = (Amount) o;
        return value == price.value;
    }

    @Override
    public int hashCode() {
        return Objects.hash(value);
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }

    @Override
    public int compareTo(@NonNull Amount o) {
        return Comparator.comparing(Amount::getValue)
                .compare(this, o);
    }

    public Amount plus(Amount addPrice) {
        return new Amount(this.value + addPrice.getValue());
    }

    public Amount minus(Amount minusPrice) {
        long afterPrice = this.value - minusPrice.getValue();
        return new Amount(afterPrice);
    }

    public double toYuan() {
        return DoubleCalculator.divideDouble(value,100,2, RoundingMode.DOWN);
    }


    public boolean isPositive() {
        return value > 0;
    }

}
